package com.bosssoft.hr.train.j2se.collection;/*
 * @Auther:hantianbing
 * @Date:2022/12/2
 * @Description:TreeSetDemo
 */

import com.bosssoft.hr.train.j2se.pojo.entity.User;
import com.bosssoft.hr.train.j2se.util.CreatUser;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @ClassName TreeSetDemo
 * @Description TreeSetDemo练习
 * @Author hantianbing
 * @Date 2022/12/2 21:17
 **/
@Slf4j
public class TreeSetDemo {
    /**
     * 声明一个TreeSet， 并且注入比较器
     */
    private TreeSet<User> treeSet = new TreeSet<>(new Comparator<User>() {
        @Override
        public int compare(User u1, User u2) {
            return u1.getCode().compareTo(u2.getCode());
        }
    });

    /**
     * 排序操作
     * @param userArray
     * @return
     */
    public User[] sort(User[] userArray) {
        if(null!=userArray && userArray.length>0){
            //添加时set中的比较器会自动比较
            Collections.addAll(treeSet,userArray);
            User[] users= treeSet.toArray(new User[treeSet.size()]);
            log.info(Constraint.LOG_TAG+"{}","排序成功");
            return users;
        }else{
            return userArray;
        }
    }


}
